import { observable, action, toJS } from 'mobx';
import * as FileView from '../apis/fileView';
import { WeaTableNew } from 'comsMobx';

const { TableStore } = WeaTableNew;
import { WeaTools } from 'ecCom';

const { watermark } = WeaTools;

export default class FileViewStore {
  @observable loading= false;
  @observable basicInfo= {};
  @observable rightMenus= [];
  @observable docAccSave= {};
  @observable tableStore = new TableStore();
  constructor() {
    const { prevname, title, versionname } = window.__preTitleObj || {};
    this.basicInfo = {
      prevname,
      imagefilename: title,
      versionname,
    };
  }

  @action
  // 查看附件 基本信息
  getBasic = (params = '') => {
    this.loading = true;
    FileView.getBasic(params).then(action((data) => {
      this.loading = false;
      this.basicInfo = data.data;
      this.rightMenus = data.rightMenus;
      if (data.secWmSet.isopenwater !== '1' || data.secWmSet.wmforview !== '1') {
        setTimeout(() => {
          watermark.destory();
        }, 200);
      }
    }));
  };

  // 查看附件 替换文件
  @action
  docAccReplace = (params = {}) => {
    this.loading = true;
    FileView.docAccReplace(params).then(action((data) => {
      this.loading = false;
      if (data.url) window.location.href = data.url;
    }));
  };

  // 修改共享条件确认
  @action
  convertFile = (params = {}) => {
    FileView.convertFile(params).then(action((data) => {
      window.afterConvertFile(data);
    }));
  };

  @action
  docAccSave = (params = {}, callback) => {
    FileView.docAccSave(params).then(action((data) => {
      if (data.status == 1 && data.url) window.location.href = data.url;
      callback();
    }));
  }
  @action
  doSearch = (params) => {
    this.loading = true;
    FileView.getTable(params).then(action((data) => {
      this.tableStore.getDatas(data.sessionkey, params.current || 1);
      this.loading = false;
    })).catch(err => message.error(err));
  }
}
